from time import sleep
from selenium import webdriver
from selenium.webdriver.support.select import Select
from random import randint
driver=webdriver.Firefox()

driver.get('http://localhost/lfl/index.asp')
ipt_list=driver.find_elements_by_tag_name('input')
print(len(ipt_list))
# 获取标签值get_attribute
for ele in ipt_list:
    print(ele.get_attribute('name'))

ipt_list[0].send_keys('admin')
ipt_list[1].send_keys('admin')
ipt_list[2].click()
# 左侧构架
driver.switch_to.frame('left')
xt_set='.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)'
#点击系统设置
driver.find_element_by_css_selector(xt_set).click()
# 点击单位管理
danwei_gl='#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
driver.find_element_by_css_selector(danwei_gl).click()

sleep(2)
# 返回默认框架
driver.switch_to.default_content()
# 点击右侧框架
driver.switch_to.frame('right')
# 删除单位
driver.find_element_by_name('ID').click()
del_btn='input.button:nth-child(2)'
driver.find_element_by_css_selector(del_btn).click()
sleep(2)
driver.switch_to.alert.accept()
sleep(2)
# 添加单位
add_btn='input.button:nth-child(1)'
driver.find_element_by_css_selector(add_btn).click()
driver.find_element_by_name('danwei').send_keys('kg')
sleep(2)
driver.find_element_by_name('submit').click()
driver.switch_to.alert.accept()
sleep(2)
# 返回默认框架
driver.switch_to.default_content()
# 进入左侧框架
driver.switch_to.frame('left')
# 添加大类
sleep(2)
dalei_gl='#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
driver.find_element_by_css_selector(dalei_gl).click()
# 返回默认框架
driver.switch_to.default_content()
# 进入右侧框架
driver.switch_to.frame('right')
# add_btn2='/html/body/table/tbody/tr[1]/td[2]/table/tbody/tr/td[2]/input'
# driver.find_element_by_xpath(add_btn2).click()
btn_list=driver.find_elements_by_tag_name('input')
print(len(btn_list))
for ele2 in btn_list:
    print(ele2.get_attribute('value'))
btn_list[0].click()
driver.find_element_by_name('bigclass').send_keys('lingjian')
sleep(2)
driver.find_element_by_name('submit').click()
driver.switch_to.alert.accept()
sleep(2)
# 返回默认框架
driver.switch_to.default_content()
# 进入左侧框架
driver.switch_to.frame('left')
# 添加小类
xiaolei_gl='#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
driver.find_element_by_css_selector(xiaolei_gl).click()
# 返回默认框架
driver.switch_to.default_content()
# 进入右侧框架
driver.switch_to.frame('right')
# 创建下拉列表
sel=driver.find_element_by_name('bigclass')
lst=Select(sel)
# 获取列表数量
lst_count=len(lst.options)
# 随机生成列表序号
rand = randint(1, lst_count-1)
# 通过随机产生的序号，选择一个列表项
lst.select_by_index(rand)
add_xl='input.button:nth-child(1)'
driver.find_element_by_css_selector(add_xl).click()
driver.find_element_by_name('smallclass').send_keys('gki')
sleep(2)
sure_btn=('input.button:nth-child(1)')
driver.find_element_by_css_selector(sure_btn).click()
driver.switch_to.alert.accept()
sleep(2)
# 返回主架构
driver.switch_to.default_content()
# 进入上侧架构,退出系统
driver.switch_to.frame('topFrame')
driver.find_element_by_link_text('安全退出').click()
driver.switch_to.alert.accept()
sleep(2)
driver.quit()



